package com.ksyzt.ksite2.client.news;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.ksyzt.gwt.client.common.SysResource;
import com.ksyzt.gwt.client.event.HasMessageHandlers;
import com.ksyzt.gwt.client.event.MessageEvent;
import com.ksyzt.gwt.client.event.MessageHandler;

public class PictureOperator extends Composite implements HasMessageHandlers {

	private static PictureOperatorUiBinder uiBinder = GWT
			.create(PictureOperatorUiBinder.class);

	interface PictureOperatorUiBinder extends UiBinder<Widget, PictureOperator> {
	}

	public HandlerRegistration addMessageHandler(MessageHandler handler) {
		if (m_register != null) {
			m_register.removeHandler();
		}
		m_register = addHandler(handler, MessageEvent.TYPE);
		return m_register;
	}

	private ClickHandler m_edit_handler = new ClickHandler() {

		@Override
		public void onClick(ClickEvent event) {
			MessageEvent e = new MessageEvent(MessageEvent.EDIT, m_data);
			fireEvent(e);

		}
	};

	private ClickHandler m_delete_handler = new ClickHandler() {

		@Override
		public void onClick(ClickEvent event) {
			MessageEvent e = new MessageEvent(MessageEvent.DELETE, m_data);
			fireEvent(e);
		}
	};

	public PictureOperator() {
		initWidget(uiBinder.createAndBindUi(this));
		imgEdit.addClickHandler(m_edit_handler);
		imgDelete.addClickHandler(m_delete_handler);
		imgEdit.setUrl(SysResource.INSTANCE.edit().getSafeUri());
		imgDelete.setUrl(SysResource.INSTANCE.delete().getSafeUri());
	}

	HandlerRegistration m_register = null;

	@UiField
	Image imgEdit;

	@UiField
	Image imgDelete;

	Object m_data;

	public void setData(Object data) {
		m_data = data;
	}

}
